package com.viphuli.fragment;

import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

/**
 * 私人定制
 * 
 * @author li.li
 *
 */
public class HomePrivateCustomFragment extends BaseProgressFragment {

	@Bind(R.id.id_main_content_service_private_custom_phone)
	protected EditText phoneText;

	@Bind(R.id.id_main_content_service_private_custom_age)
	protected EditText ageEText;

	@Bind(R.id.id_main_content_service_private_custom_sex)
	protected RadioGroup sexRadioGroup;

	@Bind(R.id.id_main_content_service_private_custom_content)
	protected EditText customContentEText;

	@Bind(R.id.id_main_content_service_private_custom_need_service)
	protected EditText needServiceEText;

	@Bind(R.id.id_main_content_service_private_custom_remark)
	protected EditText customRemarkEText;

	@Bind(R.id.id_main_content_service_private_custom_order_btn)
	protected TextView orderBtn;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home_private_custom;
	}

	@Override
	protected void initParams() {
	}

	@Override
	protected void initView() {
	}

	@Override
	protected void initData() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
		if (tokenKeeper.isLogin()) {

			//网络请求
			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			ApiRequest.homePrivateCustomInfo.request(this, params, HandlerType.waitting, false);

		}

	}

	@OnClick(R.id.id_main_content_service_private_custom_order_btn)
	public void submit() {

		String phone = phoneText.getText().toString();
		String age = ageEText.getText().toString();
		String content = customContentEText.getText().toString();
		String needService = needServiceEText.getText().toString();
		String remark = customRemarkEText.getText().toString();

		int checkedId = sexRadioGroup.getCheckedRadioButtonId();
		RadioButton radioButton = (RadioButton) sexRadioGroup.findViewById(checkedId);
		String sex = radioButton != null ? (String) radioButton.getTag() : null;

		if (StringUtils.isBlank(phone)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_phone), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(age)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_age), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(sex)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_sex), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(content)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_content), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(needService)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_need_service), Toast.LENGTH_SHORT).show();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("tel", phone);
		params.put("age", age);
		params.put("sex", sex);
		params.put("desc", content);
		params.put("need_service", needService);

		if (StringUtils.isBlank(remark)) {
			params.put("remark", "");
		} else {
			params.put("remark", remark);
		}

		ApiRequest.homePrivateCustom.request(this, params);
	}

	public EditText getPhoneText() {
		return phoneText;
	}

}
